package luren.king.wechatlite.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import luren.king.wechatlite.R;
import luren.king.wechatlite.activity.ScanQRCodeActivity;
import luren.king.wechatlite.adapter.ViewPagerAdapter;
import luren.king.wechatlite.config.Config;

public class MainActivity extends AppCompatActivity{
    ViewPager2 viewPager;
    BottomNavigationView navigationView;
    MenuItem menuItem;
    TextView title;
    ConstraintLayout toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        // 注册全局异常处理类
//        CrashExceptionHandler crashExceptionHandler = CrashExceptionHandler.newInstance();
//        crashExceptionHandler.init(getApplicationContext());


        //初始化活动
        Config.MAIN_ACTIVITY = this;
        //初始化webSocket
        Config.WEB_SOCKET.startWebsocket();


        //使用BottomNavigationView+ViewPager2实现导航栏功能
        init();
    }


    private void init() {
        //上面的状态栏
        title = findViewById(R.id.title);
        //添加按钮
        ImageView addBtn = findViewById(R.id.add_button);
        //添加弹窗
        addBtn.setOnClickListener(v -> {
            View view = getLayoutInflater().inflate(R.layout.add_friends, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(this)
                    .setTitle("添加好友")
                    .setView(view);
            builder.create().show();
            Button search = view.findViewById(R.id.search_add);
            search.setOnClickListener(v1 -> {
                Toast.makeText(this, "这里显示搜索列表", Toast.LENGTH_LONG).show();
            });
        });
        ImageView search = findViewById(R.id.search_button);
        search.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(this, ScanQRCodeActivity.class);
            startActivity(intent);
        });
        //工具栏标题
        toolbar = findViewById(R.id.toolbar);
        //工具栏布局
        ViewPagerAdapter adapter = new ViewPagerAdapter(this);
        //页面适配器
        viewPager = findViewById(R.id.viewPager);
        navigationView = findViewById(R.id.nav_view);
        navigationView.setOnItemSelectedListener(mOnItemSelectedListener);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int p) {
                menuItem = navigationView.getMenu().getItem(p);
                menuItem.setChecked(true);
            }
        });
        viewPager.setAdapter(adapter);
        //设置适配器
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            //设置滑动页面出现的操作
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        title.setText("聊天");
                        toolbar.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        toolbar.setVisibility(View.VISIBLE);
                        title.setText("通讯录");
                        break;
                    case 2:
                        toolbar.setVisibility(View.VISIBLE);
                        title.setText("发现");
                        break;
                    case 3:
                        toolbar.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    private final NavigationBarView.OnItemSelectedListener mOnItemSelectedListener
            = new NavigationBarView.OnItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            menuItem = item;
            if (item.getItemId() == R.id.Message) {
                toolbar.setVisibility(View.VISIBLE);
                title.setText("聊天");
                viewPager.setCurrentItem(0);
                return true;
            }
            else if (item.getItemId() == R.id.Contacts) {
                toolbar.setVisibility(View.VISIBLE);
                title.setText("通讯录");
                viewPager.setCurrentItem(1);
                return true;
            }
            else if (item.getItemId() == R.id.Find) {
                toolbar.setVisibility(View.VISIBLE);
                title.setText("发现");
                viewPager.setCurrentItem(2);
                return true;
            }
            else if (item.getItemId() == R.id.Mine) {
                viewPager.setCurrentItem(3);
                toolbar.setVisibility(View.GONE);
                return true;
            }
            else {
                return false;
            }
        }
    };

}